﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NBusiness.CodeDom;
using NBusiness.ESharp.Compiler;
using NBusiness.CodeDom.Compiler;

namespace NBusiness.ESharp
{
    [Serializable]
    public class ESharpModelInherit : EntityModelInherit
    {
        protected override void InitializeAssignmentLogic()
        {
            Assignments.Add(new AssignmentLogic(
                ESharpEntityElementTypes.ModelInheritType,
                SetInheritType));

            Assignments.Add(new AssignmentLogic(
                ESharpEntityElementTypes.ModelInheritTypeName,
                SetInheritName));

            Assignments.Add(new AssignmentLogic(
                ESharpEntityElementTypes.StatementEnd,
                PostAssignmentAction.Stop));

            Assignments.Add(new AssignmentLogic(ESharpEntityElementTypes.WhiteSpace));
            Assignments.Add(new AssignmentLogic(ESharpEntityElementTypes.Comment));
        }

        protected override void Attach()
        {
            // do nothing
        }

        private void SetInheritType(object sender, AssignmentLogicEventArgs e)
        {
            switch (e.Elements.Peek().Value)
            {
                case "field":
                    Type = EntityModelInheritType.Field;
                    break;
                case "relationship":
                    Type = EntityModelInheritType.Relationship;
                    break;
                case "action":
                    Type = EntityModelInheritType.Action;
                    break;
            }
        }

        private void SetInheritName(object sender, AssignmentLogicEventArgs e)
        {
            if (string.IsNullOrEmpty(Name))
            {
                Name = e.Elements.Peek().Value;
            }
            else
            {
                Name += "." + e.Elements.Peek().Value;
            }
        }
    }
}
